package com.example.phpclient.student;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.phpclient.student.Calendar_day_textView;
import com.example.phpclient.R;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;

public class NewCalendar extends LinearLayout {
    private ImageView btn_pre;
    private ImageView btn_next;
    private TextView txtDate;
    private GridView gridView;

    private Calendar curDate=Calendar.getInstance();
//    private String displayFormat;
//    public NewCalendarListener listener;

    public NewCalendar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initControl(context,attrs);
    }

    public NewCalendar(Context context) {
        super(context);
    }

    public NewCalendar(Context context, AttributeSet attrs) {
        super(context, attrs);
        initControl(context,attrs);
    }
     private void initControl(Context context,AttributeSet attrs){
        bindControl(context);//绑定控件
        bindControlEvent();//绑定事件

//         TypedArray ta = getContext().obtainStyledAttributes(attrs,R.styleable.NewCalendar);
//
//         try {
//             String format = ta.getString(R.styleable.NewCalendar_dateFormat);//系统拼接字符串
//             displayFormat = format;
//             if (displayFormat == null)
//             {
//                 displayFormat="MMM yyyy";
//             }
//         }finally {
//             ta.recycle();//回收
//         }
         renderCalendar();//渲染
    }

    private void bindControlEvent() {
        btn_pre.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                curDate.add(Calendar.MONTH,-1);
                renderCalendar();
            }
        });
        btn_next.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                curDate.add(Calendar.MONTH,+1);
                renderCalendar();
            }
        });
    }

    private void bindControl(Context context){
        LayoutInflater inflater = LayoutInflater.from(context);
        inflater.inflate(R.layout.rl_fragment,this);
        btn_pre=(ImageView)findViewById(R.id.btn_pre);
        btn_next=(ImageView)findViewById(R.id.btn_next);
        txtDate=(TextView) findViewById(R.id.txtDate);
        gridView=(GridView)findViewById(R.id.calendar_grid);
    }
    private void renderCalendar(){
        SimpleDateFormat sdf =new SimpleDateFormat("MMM yyy");
        txtDate.setText(sdf.format(curDate.getTime()));

        ArrayList<Date> cells=new ArrayList<>();
        Calendar calendar = (Calendar) curDate.clone();

        calendar.set(Calendar.DAY_OF_MONTH,1);//置于每月第一天
        final int preDays= calendar.get(Calendar.DAY_OF_WEEK)-1;
        calendar.add(Calendar.DAY_OF_MONTH,-preDays);

        int maxCellCount = 6*7;
        while (cells.size()<maxCellCount)
        {
            cells.add(calendar.getTime());
            calendar.add(Calendar.DAY_OF_MONTH,1);
        }

        gridView.setAdapter(new CalendarAdapter(getContext(),cells));
//        gridView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
//            @Override
//            public boolean onItemLongClick(AdapterView<?> parent, View view, int i, long l) {
//                if (listener == null){
//                    return false;
//                }else {
//                   listener.onItemLongPress((Date)parent.getItemAtPosition(pos);
//                   return true;
//                }
//
//            }
//        });
    }
    private class CalendarAdapter extends ArrayAdapter<Date>{
        LayoutInflater inflater;
        public CalendarAdapter(Context context,ArrayList<Date>days) {
            super(context, R.layout.calendar_text_day,days);
            inflater=LayoutInflater.from(context);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            Date date = getItem(position);
            if (convertView == null){
                convertView = inflater.inflate(R.layout.calendar_text_day,parent,false);
            }
            int day =date.getDate();
            ((TextView)convertView).setText(String.valueOf(day));//convertview赋值

            Date now= new Date();
            boolean isTheSameMonth =false;
            if (date.getMonth() == now.getMonth())
            {
                isTheSameMonth = true;
            }
            if (isTheSameMonth)
            {
                ((TextView)convertView).setTextColor(Color.parseColor("#000000"));
            }else
            {
                ((TextView)convertView).setTextColor(Color.parseColor("#7F7F7F"));
            }


            if (now.getDate() == date.getDate()&&now.getMonth()==date.getMonth()&&now.getYear()==date.getYear())
            {
                ((TextView)convertView).setTextColor(Color.parseColor("#ff0000"));
                ((Calendar_day_textView)convertView).isToday = true;
            }
            return convertView;
        }


    }
//    public interface NewCalendarListener
//    {
//        void onItemLongPress(Date date);
//    }
}
